package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.media.service.IWmNewsMaterialService;
import com.sun.deploy.net.proxy.pac.PACFunctionsImpl;
import io.netty.handler.ipfilter.IpFilterRule;
import org.checkerframework.checker.units.qual.min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-15
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    // 注入MinIOService
    @Autowired
    private MinIOService minIOService;

    @Autowired
    private IWmNewsMaterialService wmNewsMaterialService;

    @Override
    public ResponseResult upload(MultipartFile file) {

        User user = MediaThreadLocalUtil.get();

        if (user == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        try {
            String url = minIOService.upload(file);

            WmMaterial wmMaterial = new WmMaterial();

            wmMaterial.setUrl(url);

            wmMaterial.setUserId(user.getUserId());

            wmMaterial.setType(0);

            wmMaterial.setIsCollection(false);

            wmMaterial.setCreatedTime(new Date());

            this.save(wmMaterial);

            return ResponseResult.okResult(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.okResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {

        User user = MediaThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        LambdaQueryWrapper<WmMaterial> query = new LambdaQueryWrapper<>();

        // Integer isCollection = com.heima.search.dto.getIsCollection();

        if (dto.getIsCollection() != null) {

            query.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        query.eq(WmMaterial::getUserId, user.getUserId());

        query.orderByDesc(WmMaterial::getCreatedTime);

        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        IPage<WmMaterial> page1 = this.page(page, query);

        return new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
    }

    @Override
    public ResponseResult getCollection(Integer id, int type) {

        WmMaterial wmMaterial = this.getById(id);

        if (wmMaterial == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (type == 1) {

            LambdaUpdateWrapper<WmMaterial> update = new LambdaUpdateWrapper<>();

            update.eq(WmMaterial::getId, id);

            update.set(WmMaterial::getIsCollection, true);

            this.update(update);

            return ResponseResult.okResult(id);
        } else {

            LambdaUpdateWrapper<WmMaterial> update = new LambdaUpdateWrapper<>();

            update.eq(WmMaterial::getId, id);

            update.set(WmMaterial::getIsCollection, false);

            this.update(update);

            return ResponseResult.okResult(id);
        }
    }

    @Override
    public ResponseResult deleteMaterial(Integer id) {

        WmMaterial wmMaterial = this.getById(id);

        if (wmMaterial == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        LambdaQueryWrapper<WmNewsMaterial> query = new LambdaQueryWrapper<>();

        query.eq(WmNewsMaterial::getMaterialId, id);

        List<WmNewsMaterial> list = wmNewsMaterialService.list(query);

        if (!CollectionUtils.isEmpty(list)) {

            return ResponseResult.errorResult(AppHttpCodeEnum.MERTIRAL_NOT_DELETE);
        }
        this.removeById(id);

        return ResponseResult.okResult(id);
    }
}
